﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESF.Web;
using ESF.SSO.Web.Tools.Modules;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.General;
using ESF.SSO.Core.BusinessLogic;
using ESF.Web.ServerControls;
using ESF.SSO.Core.Entities;
using ESF.SSO.Presenter.Client.ContactUs;
using ESF.Web.Pages;

#endregion


public partial class ContactUsLog : BaseCustomBrowsePagedPage<ESF.SSO.Core.Entities.ContactUs, ContactUsLogPresenter, IContactUsLogViewer>, IContactUsLogViewer
{
	#region Campos del Interface

	public bool? filWithResponse
	{
		get
		{
			return TextHelper.GetNullableBool(ddlWithResponse.SelectedValue);
		}
		set
		{
			ListLoader.Select(ddlWithResponse, value);
		}
	}


	public int? filContactUsTypeID
	{
		set { ListLoader.Select(ddlContactUsType, value); }
		get { return TextHelper.GetNullableInt(ddlContactUsType.SelectedValue); }
	}


	public string filSenderEmail
	{
		get
		{
			return lblSenderEmail.Text;
		}
	}

	#endregion


    #region Page Events
	
	protected void Page_Load(object sender, EventArgs e)
	{
		_Presenter = new ContactUsLogPresenter (this); 
		
		if (!IsPostBack)
		{
			lblSenderEmail.Text = AuthenticationModule.SignedInUser.Email;

			ListLoader.LoadList(ddlContactUsType, WebGeneral.ListBasicReference(BasicReferenceTypes.ContactUsType));

			ListLoader.LoadYESNOList (ddlWithResponse);
		}
    }

	#endregion


	#region Funciones Sobreescritas
	
	protected override DataPagging GetDataPagging()
	{
		return dpggrvResults;
	}


	protected override GridView GetGridView()
	{
		return grvResults;
	}

	#endregion


	#region Eventos de la Grilla

	protected void grvResults_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		ESF.SSO.Core.Entities.ContactUs item;

		ESF.SSO.Core.Entities.User senderUser = null;
		ESF.SSO.Core.Entities.User replyUser = null;


		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			item = e.Row.DataItem as ESF.SSO.Core.Entities.ContactUs;

			if (!string.IsNullOrEmpty(item.filUserAlias))
			{
				senderUser = CtrlUser.Get(new ESF.SSO.Core.Entities.User() { Alias = item.filUserAlias });
			}

			if (!string.IsNullOrEmpty(item.ResponseUserAlias))
			{
				replyUser = CtrlUser.Get(new ESF.SSO.Core.Entities.User() { Alias = item.ResponseUserAlias });
			}

			if (senderUser != null)
			{
				(e.Row.FindControl("lblUserAlias") as Label).Text = TextHelper.FormatIfValueNotEmpty("<b>Usuario:</b> {0}<br/>", senderUser.Alias);
				(e.Row.FindControl("lblCalUserFullName") as Label).Text = TextHelper.FormatIfValueNotEmpty("<b>Nombre:</b> {0}<br/>", senderUser.calFullName);
			}

			if (replyUser != null)
			{
				(e.Row.FindControl("lblResponseUserAlias") as Label).Text = TextHelper.FormatIfValueNotEmpty("<b>Tramitado por:</b> {0}<br/>", replyUser.Alias);
				(e.Row.FindControl("lblResponseCalUserFullName") as Label).Text = TextHelper.FormatIfValueNotEmpty("<b>Nombre:</b> {0}<br/>", replyUser.calFullName);
			}

			(e.Row.FindControl("lblSenderEmail") as Label).Text = item.SenderEmail;
			(e.Row.FindControl("lblCreationDateTime") as Label).Text = item.CreationDateTime.Value.ToString("yyyy-MM-dd h:mm:ss tt");
			(e.Row.FindControl("lblTitle") as Label).Text = TextHelper.FormatIfValueNotEmpty("<b>Asunto:</b> {0}<br/>", item.Title);
			(e.Row.FindControl("hidTitle") as HiddenField).Value = item.Title;

			(e.Row.FindControl("lblContactUsType") as Label).Text = TextHelper.FormatIfValueNotEmpty("<b>Categoria:</b> {0}<br/>", item.ContactUsTypeID.Name);

			(e.Row.FindControl("lblResponseDateTime") as Label).Text = TextHelper.FormatIfValueNotEmpty("<b>Fecha y Hora:</b> {0:yyyy-MM-dd h:mm:ss tt}<br/>", item.ResponseDateTime);

			(e.Row.FindControl("txtResponse") as TextBox).Text = item.Response;
			
			(e.Row.FindControl("txtComments") as TextBox).Text = item.Comments;
		}
	}

	#endregion
}